package com.bike.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhOrder;
import com.bike.entity.HhServiceConfig;
import com.bike.mapper.HhServiceConfigMapper;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhServiceConfigServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhServiceConfigMapper hhServiceConfigMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhServiceConfig record) {
		if(pd.getLong("serviceConfigId")>0){
			 //编辑
			hhServiceConfigMapper.updateByPrimaryKeySelective(record);
		}else{
			hhServiceConfigMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhServiceConfigById(long serviceConfigId) {
		return hhServiceConfigMapper.deleteByPrimaryKey(serviceConfigId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhServiceConfigList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		 
		List<HhServiceConfig> records= hhServiceConfigMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhServiceConfig>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhServiceConfig> listAll(PageData pd) {
		return  hhServiceConfigMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhServiceConfig selectByHhServiceConfigId(Long serviceConfigId) {
		return hhServiceConfigMapper.selectByPrimaryKey(serviceConfigId);
	}

	public HhServiceConfig selectServciFee(HhOrder order2, PageData pd) {
		List<HhServiceConfig>  serviceConfigs=hhServiceConfigMapper.selectAll(pd);//商家配置
		int nowHours=TimeManager.getInstance().getHour();
		HhServiceConfig service=new HhServiceConfig();
		HhServiceConfig defaultServiceConfig=null;
		int flag=0;
		for(HhServiceConfig serviceConfig:serviceConfigs){
			if(serviceConfig.getStartHours()<0||serviceConfig.getEndHours()>24||serviceConfig.getEndHours()<0||serviceConfig.getEndHours()>24){
				defaultServiceConfig=serviceConfig;
			}else{
				//时间段之内的手续费
				if(nowHours>=serviceConfig.getStartHours()&&nowHours<serviceConfig.getEndHours()){
					service=serviceConfig;
					flag=1;
					break;
				}
			}
			
		}
		if(flag==0){
			if(defaultServiceConfig!=null){
				return defaultServiceConfig;
			}else{
				pd.put("businessId", 0);
				serviceConfigs=hhServiceConfigMapper.selectAll(pd);//系统默认
				for(HhServiceConfig serviceConfig:serviceConfigs){
					if(serviceConfig.getStartHours()<0||serviceConfig.getEndHours()>24||serviceConfig.getEndHours()<0||serviceConfig.getEndHours()>24){
						defaultServiceConfig=serviceConfig;
					}
					//时间段之内的手续费
					if(nowHours>=serviceConfig.getStartHours()&&nowHours<serviceConfig.getEndHours()){
						service=serviceConfig;
						flag=1;
						break;
					}
				}
				if(flag==0){
					if(defaultServiceConfig!=null){
						return defaultServiceConfig;
					}
				}
			}
		} 
		
		return service;
	}
}

